package BookFrame;

import javax.swing.*;
import java.awt.*;

public class BookLayoutDemo extends JFrame {
    private JPanel searchPanel = new JPanel();    // 搜索面板
    private JPanel categoryPanel = new JPanel();  // 分类面板
    private JPanel operationPanel = new JPanel(); // 操作面板
    private JPanel quickAccessPanel = new JPanel(); // 快捷入口面板

    public BookLayoutDemo() {
        setSize(800, 600);
        setLocation(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("图书管理系统 - 界面布局示例");

        // 初始化各面板布局
        layoutSearchPanel();
        layoutCategoryPanel();
        layoutOperationPanel();
        layoutQuickAccessPanel();

        // 顶层布局：2行2列网格布局
        setLayout(new GridLayout(2, 2));
        add(searchPanel);
        add(categoryPanel);
        add(operationPanel);
        add(quickAccessPanel);
    }

    /** 搜索面板（BorderLayout） */
    private void layoutSearchPanel() {
        JButton searchBtn = new JButton("搜索图书");
        JTextField searchField = new JTextField(20);
        JLabel promptLabel = new JLabel("按书名/作者/ISBN搜索：");

        searchPanel.setLayout(new BorderLayout());
        searchPanel.add(promptLabel, BorderLayout.WEST);
        searchPanel.add(searchField, BorderLayout.CENTER);
        searchPanel.add(searchBtn, BorderLayout.EAST);
    }

    /** 分类面板（FlowLayout） */
    private void layoutCategoryPanel() {
        JButton literatureBtn = new JButton("文学类");
        JButton scienceBtn = new JButton("科技类");
        JButton educationBtn = new JButton("教育类");
        JButton historyBtn = new JButton("历史类");

        categoryPanel.setLayout(new FlowLayout(FlowLayout.LEADING)); // 左对齐流式布局
        categoryPanel.add(new JLabel("图书分类："));
        categoryPanel.add(literatureBtn);
        categoryPanel.add(scienceBtn);
        categoryPanel.add(educationBtn);
        categoryPanel.add(historyBtn);
    }

    /** 操作面板（GridLayout 2x2） */
    private void layoutOperationPanel() {
        JButton borrowBtn = new JButton("办理借阅");
        JButton returnBtn = new JButton("办理归还");
        JButton renewBtn = new JButton("图书续借");
        JButton reserveBtn = new JButton("图书预约");

        operationPanel.setLayout(new GridLayout(2, 2, 10, 10)); // 2行2列，间距10px
        operationPanel.add(borrowBtn);
        operationPanel.add(returnBtn);
        operationPanel.add(renewBtn);
        operationPanel.add(reserveBtn);
    }

    /** 快捷入口面板（无布局管理器） */
    private void layoutQuickAccessPanel() {
        JButton adminBtn = new JButton("管理员入口");
        JTextField quickSearch = new JTextField("快速查找图书...");

        quickAccessPanel.setLayout(null); // 绝对布局
        quickSearch.setBounds(20, 20, 250, 30); // x=20, y=20, 宽250, 高30
        adminBtn.setBounds(20, 70, 120, 35);

        quickAccessPanel.add(quickSearch);
        quickAccessPanel.add(adminBtn);
    }

    public static void main(String[] args) {
        new BookLayoutDemo().setVisible(true);
    }
}
